package cn.szsys.integration.dos;

import cn.szsys.integration.runner.entity.Accessor;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.Arrays;
import java.util.Date;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 下游配置
 *
 * @TableName accessor
 */
@TableName(value = "accessor")
@Data
public class AccessorDO {
    /**
     * id
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * ak
     */
    private String ak;

    /**
     * aks
     */
    private String aks;

    /**
     * name
     */
    private String name;

    /**
     * 备注
     */
    private String remark;

    /**
     * 到期时间的时间戳
     */
    private Long expireTime;

    /**
     * 接口权限, 多个用‘,’隔开
     */
    private String apiPermissions;

    public Accessor toAccessor() {
        Accessor accessor = new Accessor();
        accessor.setAk(this.ak);
        accessor.setAks(this.aks);
        accessor.setName(this.name);
        accessor.setRemark(this.remark);
        if (null != this.expireTime) {
            accessor.setExpireTime(new Date(this.expireTime));
        }
        Set<String> apiPermissions = Arrays.stream(this.apiPermissions.split(","))
                .collect(Collectors.toSet());
        accessor.setApiPermissions(apiPermissions);
        return accessor;
    }

}